package com.ndp.fb.rdb.mongo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSONObject;
import com.mongodb.*;
import com.ndp.fb.constant.MongoConstants;
import com.ndp.fb.mongo.api.FaceBookJobMongoService;
import com.ndp.fb.mongo.model.FaceBookJob;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class FaceBookJobMongoServiceImpl implements FaceBookJobMongoService {
    Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private DB mongoDBClient;
    @Autowired
    private DB mongoDBClientPrimary;
    
    @Override
    public void saveFaceBookJob(FaceBookJob job) {
        if (job == null) {
            return;
        }
        DBCollection dbCollection = mongoDBClientPrimary.getCollection(MongoConstants.TABLE_NAME_FACEBOOK_JOB);
        DBObject o = (DBObject) com.mongodb.util.JSON.parse(JSONObject.toJSONString(job));
        if (o.containsField("start_time")) {
            o.put("start_time", new Date(Long.valueOf(o.get("start_time").toString())));
        }
        dbCollection.insert(o,WriteConcern.JOURNALED);
    }

    @Override
    public void updateFaceBookJobStatus(String job_id, String job_status, Object result) {
        DBCollection dbCollection = mongoDBClientPrimary.getCollection(MongoConstants.TABLE_NAME_FACEBOOK_JOB);
        DBObject o = new BasicDBObject();
        o.put("job_status", job_status);
        o.put("result", result);
        if (job_status.equals("success") || job_status.equals("fail")) {
            o.put("end_time", new Date());
        }
        dbCollection.update(new BasicDBObject("job_id", job_id), new BasicDBObject("$set", o),false,false,WriteConcern.JOURNALED);
    }

    @Override
    public void addNewPageStatus(String job_id, Object pageStatus) {
        DBCollection dbCollection = mongoDBClientPrimary.getCollection(MongoConstants.TABLE_NAME_FACEBOOK_JOB);
        DBObject q = new BasicDBObject();
        q.put("job_id", job_id);
        DBObject o = new BasicDBObject();
        DBObject object = (DBObject) com.mongodb.util.JSON.parse(JSONObject.toJSONString(pageStatus));
        if (object.containsField("end_time")) {
            object.put("end_time", new Date(Long.valueOf(object.get("end_time").toString())));
        }
        o.put("$each", new Object[]{object});
        o.put("$position", 0);
        dbCollection.update(q, new BasicDBObject("$push", new BasicDBObject("page_status", o)),false,false,WriteConcern.JOURNALED);
    }

    @Override
    public List<FaceBookJob> getJobByJobTypeStatus(String jobType, String status) {
        DBCollection dbCollection = mongoDBClientPrimary.getCollection(MongoConstants.TABLE_NAME_FACEBOOK_JOB);
        DBObject o = new BasicDBObject();
        o.put("job_type", jobType);
        o.put("job_status", status);
        List<FaceBookJob> faceBookJobs = new ArrayList<>();
        DBCursor dbCursor = dbCollection.find(o);
        while (dbCursor.hasNext()) {
            faceBookJobs.add(JSONObject.parseObject(JSONObject.toJSONString(dbCursor.next()), FaceBookJob.class));
        }
        return faceBookJobs;
    }

    @Override
    public FaceBookJob getFaceBookJob(String jobId) {
        DBCollection dbCollection = mongoDBClientPrimary.getCollection(MongoConstants.TABLE_NAME_FACEBOOK_JOB);
        DBObject dbObject = dbCollection.findOne(new BasicDBObject("job_id", jobId));
        return JSONObject.parseObject(JSONObject.toJSONString(dbObject), FaceBookJob.class);
    }

}
